﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DAL;
using System.Data;
namespace BAL
{
    public static class BAL_BANGCHIETKHAU
    {
        public static QuanLyTiemNetDataContext db = new QuanLyTiemNetDataContext();
        //them
        public static bool ThemBCK(ref string err, string MaLoaiKH, string NgayCapNhat, float ChietKhau)
        {
            bool them = false;
            try
            {
                BANGCHIETKHAU bck = new BANGCHIETKHAU();
                bck.MaLoaiKH = MaLoaiKH;
                bck.NgayCapNhat = NgayCapNhat;
                bck.ChietKhau = ChietKhau;
                bck.DaHuy = false;
                db.BANGCHIETKHAUs.InsertOnSubmit(bck);
                db.SubmitChanges();
                them = true;

            }
            catch (Exception e)
            {
                err = e.Message.ToString();
            }
            return them;
        }
        //sua
        public static bool SuaBCK(ref string err, int MaBCK, string NgayCapNhat, float ChietKhau)
        {
            bool sua = false;
            try
            {
                BANGCHIETKHAU tim = db.BANGCHIETKHAUs.Single(ck => ck.MaBCK.Equals(MaBCK));
                if (tim != null)
                {
                    
                    //tim.MaLoaiKH = MaLoaiKH;
                    tim.NgayCapNhat = NgayCapNhat;
                    tim.ChietKhau = ChietKhau;
                    db.SubmitChanges();
                    sua = true;
                }


            }
            catch (Exception e)
            {
                err = e.Message;
            }

            return sua;
        }

        //xoa

        public static bool XoaBCK(ref string err, int MaBCK)
        {
            bool xoa = false;
            try
            {
                BANGCHIETKHAU tim = db.BANGCHIETKHAUs.Single(bck => bck.MaBCK.Equals(MaBCK));
                if (tim != null)
                {
                    db.BANGCHIETKHAUs.DeleteOnSubmit(tim);
                    db.SubmitChanges();
                    xoa = true;
                }
            }
            catch (Exception e)
            {
                err = e.Message;
            }
            return xoa;
        }

        //
        public static DataTable ConvertToDaTaTable(List<BANGCHIETKHAU> list)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("MaBCK");
            dt.Columns.Add("MaLoaiKH");
            dt.Columns.Add("NgayCapNhat");
            dt.Columns.Add("ChietKhau");
            //dt.Columns.Add("DaHuy");
            foreach (var i in list)
            {
                dt.Rows.Add(i.MaBCK, i.MaLoaiKH, i.NgayCapNhat, i.ChietKhau);
            }
            return dt;
        }

        //
        public static DataTable LayBCK()
        {
            DataTable dt = new DataTable();
            var bck = from ck in db.BANGCHIETKHAUs where ck.DaHuy==false select ck;
            dt = ConvertToDaTaTable(bck.ToList());
            return dt;
        }
        //lay lich su xoa
        public static DataTable LayLichSuBCK()
        {
            DataTable dt = new DataTable();
            var bck = from ck in db.BANGCHIETKHAUs where ck.DaHuy == true select ck;
            dt = ConvertToDaTaTable(bck.ToList());
            return dt;
        }
        //Đánh dấu xóa
        public static bool DanhDauXoa(ref string err, int MaBCK, bool value)
        {
            bool danhdauxoa = false;
            try
            {
                BANGCHIETKHAU tim = db.BANGCHIETKHAUs.Single(bck => bck.MaBCK.Equals(MaBCK));
                if (tim != null)
                {
                    tim.DaHuy = value;
                    db.SubmitChanges();
                    danhdauxoa = true;
                }
            }
            catch (Exception e)
            {
                err = e.Message;
            }
            return danhdauxoa;

        }
    }
}
